import Vue from 'vue';
//该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router';
//使用插件
Vue.use(VueRouter);
//引入路由配置信息
import routes from './routes'

//重写push|replace方法，解决点击多次报错问题
//location是跳转信息，resolve成功回调，reject失败回调
let originPush = VueRouter.prototype.push; //保存一份
let originReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function(location,resolve,reject){
    if(resolve && reject) {
        originPush.call(this,location,resolve,reject);
    }else {
        originPush.call(this,location,()=>{},()=>{});
    }
}
VueRouter.prototype.replace = function(location,resolve,reject){
  if(resolve && reject) {
    originReplace.call(this,location,resolve,reject);
  }else {
    originReplace.call(this,location,()=>{},()=>{});
  }
}

//创建router实例对象，去管理一组组的路由规则
let router = new VueRouter({
  //配置路由,对象解构写法
  routes,
  //默认滚动行为
  scrollBehavior(to,from,savedPosition){
    //y=0代表滚动条在最上网
    return {y:0}
  }
});

export default router;
